package com.valohyd.android_utils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.util.Log;

/**
 * source found here :
 * http://droidapp.co.uk/2011/05/12/android-dev-reading-a-rss-feed/
 * 
 * @author http://droidapp.co.uk/2011/05/12/android-dev-reading-a-rss-feed/
 *         modified by ValohydTeam
 */
public class RSSParser {

	public static RSSItem[] parse(String url_rss) {
		URL url;
		RSSItem[] res = null;

		try {
			// Set the url
			url = new URL(url_rss);

			// Setup the connection
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			// Connect
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {

				// Retreive the XML from the URL
				DocumentBuilderFactory dbf = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document doc;
				doc = db.parse(url.openStream());
				doc.getDocumentElement().normalize();

				// This is the root node of each section you want to parse
				NodeList itemLst = doc.getElementsByTagName("entry");

				// This sets up some arrays to hold the data parsed
				res = new RSSItem[itemLst.getLength()];

				// Loop through the XML passing the data to the arrays
				for (int i = 0; i < itemLst.getLength(); i++) {
					res[i] = new RSSItem();

					Node item = itemLst.item(i);
					if (item.getNodeType() == Node.ELEMENT_NODE) {
						Element ielem = (Element) item;

						// This section gets the elements from the XML
						// that we want to use you will need to add
						// and remove elements that you want / don't want
						NodeList title = ielem.getElementsByTagName("title");
						NodeList link = ielem.getElementsByTagName("link");
						NodeList description = ielem
								.getElementsByTagName("summary");

						// This section adds an entry to the arrays with the
						// data retrieved from above. I have surrounded each
						// with try/catch just incase the element does not
						// exist
						try {
							if (title != null
									&& title.item(0) != null
									&& title.item(0).getChildNodes() != null
									&& title.item(0).getChildNodes().item(0) != null)
								res[i].setTitle(title.item(0).getChildNodes()
										.item(0).getNodeValue());
						} catch (NullPointerException e) {
							e.printStackTrace();
						}

						try {
							if (link != null && link.item(0) != null
									&& link.item(0).getAttributes() != null) {
							}
							NamedNodeMap list = link.item(0).getAttributes();
							for(int j=0;j<list.getLength();j++){
								if(list.item(j).getNodeName().equals("href")){
									res[i].setUrl(list.item(j).getTextContent());
									Log.d("RSSPARSER",res[i].getUrl());
								}
							}
						} catch (NullPointerException e) {
							e.printStackTrace();
						}

						try {
							if (description != null
									&& description.item(0) != null
									&& description.item(0).getChildNodes() != null
									&& description.item(0).getChildNodes()
											.item(0) != null)
								res[i].setDescription(description.item(0)
										.getChildNodes().item(0).getNodeValue());
						} catch (NullPointerException e) {
							e.printStackTrace();
						}
					}
				}
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (DOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		return res;
	}
}